/*!
 * @file Game_serializer.cpp
 * @author Rocco Martino
 */
/***************************************************************************
 *   Copyright (C) 2014 by Rocco Martino                                   *
 *   martinorocco@gmail.com                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Lesser General Public License as        *
 *   published by the Free Software Foundation; either version 2.1 of the  *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU Lesser General Public License for more details.                   *
 *                                                                         *
 *   You should have received a copy of the GNU Lesser General Public      *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

/* ======================================================================= */
/* ....................................................................... */
#include <ooGame/Game>

#include <osgDB/Registry>
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
namespace {

#define ADD_LIST_READER( LIST_TYPE, OBJ_TYPE ) \
static bool check##LIST_TYPE(const ooGame::Game& game) \
{ \
    (void) game ; \
 \
    return true ; \
} \
 \
static bool write##LIST_TYPE(osgDB::OutputStream& os, const ooGame::Game& game) \
{ \
    const ooGame::LIST_TYPE&     objs = game.get##LIST_TYPE() ; \
    os << (unsigned long int)objs.size() << std::endl ; \
    for(unsigned int i=0; i<objs.size(); i++) { \
        os << objs[i].get() ; \
    } \
    return true ; \
} \
 \
static bool read##LIST_TYPE(osgDB::InputStream& is, ooGame::Game& game) \
{ \
    ooGame::LIST_TYPE    olist ; \
    unsigned int    size = 0 ; \
    is >> size ; \
    for(unsigned int i=0; i<size; i++) { \
        osg::ref_ptr<osg::Object>   tmp = is.readObject() ; \
        ooGame::OBJ_TYPE* obj = dynamic_cast<ooGame::OBJ_TYPE*>(tmp.get()) ; \
        if( obj ) { \
            olist.push_back(obj) ; \
        } \
    } \
    game.set##LIST_TYPE( olist ) ; \
    return true ; \
}

ADD_LIST_READER( SensorList,        Sensor ) ;
ADD_LIST_READER( ControllerList,    Controller ) ;
ADD_LIST_READER( ActuatorList,      Actuator ) ;

} // anon namespace
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
REGISTER_OBJECT_WRAPPER( Game,
                         new ooGame::Game,
                         ooGame::Game,
                         "osg::Object osgODE::ODECallback ooGame::Game" )
{
    ADD_UINT_SERIALIZER( State, 0 ) ;
    ADD_USER_SERIALIZER( SensorList ) ;
    ADD_USER_SERIALIZER( ControllerList ) ;
    ADD_USER_SERIALIZER( ActuatorList ) ;
}
/* ....................................................................... */
/* ======================================================================= */
